package cn.chinaunicom.sdsi.frm.workflow.sample.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.chinaunicom.sdsi.frm.workflow.sample.service.ActTestService;
import net.sf.json.JSONObject;

@Controller("businessController")
@RequestMapping(value = "/business")
public class BusinessController {

	private final Log log = LogFactory.getLog(BusinessController.class);

	/**
	 * 工作流任务相关的业务逻辑组件
	 */
	private ActTestService actTestService;

	/**
	 * 跳转至公共业务发起页面
	 * 
	 * @return 跳转路径
	 */
	@RequestMapping("/businessDetail")
	public String businessDetail() {
		log.info("跳转到业务页面模拟流程发起！");
		return "frm/workflow/sample/common-detail";
	}

	/**
	 * 跳转至流程办理的业务页面
	 * 
	 * @return 跳转路径
	 */
	@RequestMapping("/businessTask")
	public String businessTask() {
		log.info("跳转到业务页面模拟流程发起！");
		return "frm/workflow/sample/business-task";
	}

	/**
	 * 跳转至流程办理的业务自定义页面1
	 * 
	 * @return 跳转路径
	 */
	@RequestMapping("/businessTaskTab1")
	public ModelAndView businessTaskTab1() {

		ModelAndView mv = new ModelAndView();
		mv.setViewName("frm/workflow/sample/business-task-tab");
		mv.addObject("tabInfo", "自定义标签1");

		log.info("跳转到业务自定义标签1");
		return mv;
	}

	/**
	 * 跳转至流程办理的业务自定义页面2
	 * 
	 * @return 跳转路径
	 */
	@RequestMapping("/businessTaskTab2")
	public ModelAndView businessTaskTab2() {

		ModelAndView mv = new ModelAndView();
		mv.setViewName("frm/workflow/sample/business-task-tab");
		mv.addObject("tabInfo", "自定义标签2");

		log.info("跳转到业务自定义标签2");
		return mv;
	}

	@RequestMapping("/startProcess")
	@ResponseBody
	public JSONObject startProcess(String processDefinitionKey, String assignee, String taskDefinitionKey) {
		/*
		 * 在控制层处理完参数后即可调用业务模块的服务层，并注意控制事务
		 */
		System.out.println("startProcess11111=");
		actTestService.startProcess(processDefinitionKey, assignee, taskDefinitionKey);

		return new JSONObject().element("sign", "success");
	}

	/**
	 * 流程推进参数说明
	 * 
	 * @param taskDefinitionKey
	 *            目标环节定义编码
	 * @param assignee
	 *            目标环节受理人
	 * @param processInstanceId
	 *            流程实例ID
	 * @param taskId
	 *            任务ID
	 * @param comment
	 *            处理意见
	 * @param handleType
	 *            处理类型，1通过，2回退
	 * @param withdraw
	 *            此次操作是否可逆，即推进后是否可退回
	 * @return 业务数据返回
	 */
	@RequestMapping("/pushProcess")
	@ResponseBody
	public JSONObject pushProcess(String taskDefinitionKey, String assignee, String processInstanceId, String taskId,
			String comment, int handleType, int withdraw) {
		/*
		 * 在控制层处理完参数后即可调用业务模块的服务层，并注意控制事务，此处仅示例，具体所需参数业务模块自行决定
		 */
		System.out.println("pushProcess11111=");
		actTestService.pushProcess(processInstanceId, taskId, taskDefinitionKey, assignee, handleType, withdraw,
				comment);

		return new JSONObject().element("sign", "success");
	}

	@RequestMapping("/breakProcess")
	@ResponseBody
	public JSONObject breakProcess(String processInstanceId, String taskId, String comment) {
		actTestService.breakProcess(processInstanceId, taskId, comment);

		return new JSONObject().element("sign", "success");
	}

	@RequestMapping("/withdrawProcess")
	@ResponseBody
	public JSONObject withdrawProcess(String processInstanceId, String taskId) {
		actTestService.withdrawProcess(processInstanceId, taskId);

		return new JSONObject().element("sign", "success");
	}

	@Autowired
	public void setActTestService(@Qualifier("businessService") ActTestService actTestService) {
		this.actTestService = actTestService;
	}
}
